
import { Controller, Get, Post, Put, Body, Query, Param, Delete } from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { RequirePermission } from 'src/common/decorators/require-premission.decorator';
import { SurveyService } from './survey.service';
import { CreateSurveyDto, BaseSurveyDto, UpdateSurveyDto, QuerySurveyDto, ListSurveyDto } from './dto/survey.dto';
import { ApiDataResponse } from 'src/common/decorators/apiDataResponse.decorator';
import { UserTool, UserToolType } from 'src/module/system/user/user.decorator';
import { RepeatSubmit } from 'src/common/decorators/repeat-submit.decorator';
import { Operlog } from 'src/common/decorators/operlog.decorator';
import { BusinessType } from 'src/common/constant/business.constant';

@ApiTags('问卷')
@Controller('feedback/survey')
export class SurveyController {
    constructor(private readonly surveyService: SurveyService) {}
    @ApiOperation({ summary: '问卷-创建',  })
    @ApiDataResponse(BaseSurveyDto)
    @RequirePermission('feedback:survey:add')
    @Post()
    @RepeatSubmit()
    @Operlog({ businessType: BusinessType.INSERT })
    create(@Body() body: CreateSurveyDto,@UserTool() { injectCreate }: UserToolType) {
        return this.surveyService.create(injectCreate(body));
    }


    @ApiOperation({ summary: '问卷-列表',  })
    @ApiDataResponse(ListSurveyDto, true, true)
    @RequirePermission('feedback:survey:list')
    @Get('list')
    findAll(@Query() query: QuerySurveyDto) {
        return this.surveyService.findAll(query);
    }


    @ApiOperation({  summary: '问卷-详情',    })
    @ApiDataResponse(BaseSurveyDto)
    @RequirePermission('feedback:survey:query')
    @Get(':id')
    findOne(@Param('id') id: string) {
        return this.surveyService.findOne(+id);
    }


    @ApiOperation({ summary: '问卷-修改',  })
    @ApiDataResponse()
    @RequirePermission('feedback:survey:edit')
    @Put()
    @RepeatSubmit()
    @Operlog({ businessType: BusinessType.UPDATE })
    update(@Body() body: UpdateSurveyDto,@UserTool() { injectUpdate }: UserToolType) {
        return this.surveyService.update(injectUpdate(body));
    }


    @ApiOperation({  summary: '问卷-删除', })
    @ApiDataResponse()
    @RequirePermission('feedback:survey:remove')
    @Delete(':ids')
    @Operlog({ businessType: BusinessType.DELETE })
    remove(@Param('ids') ids: string) {
        const surIdList = ids.split(',').map((id) => +id);
        return this.surveyService.remove(surIdList);
    }

    @ApiOperation({  summary: '问卷-拷贝', })
    @RequirePermission('feedback:survey:add')
    @Operlog({ businessType: BusinessType.INSERT })
    @Get('/clone/:id')
    Clone(@Param('id') id: string,@UserTool() { injectCreate }: UserToolType) {
        return this.surveyService.cloneOne(+id, injectCreate({}));
    }
}
